﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pout_Porri.Screens
{
    class MenuScreen : Screen
    {
        /// <summary>
        /// Lista de menuButton. Toda vez que um botão for criado deverá ser 
        /// adcionado a esta lista
        /// </summary>
        public static List<Menu.menuButton> lstMB = new List<Menu.menuButton>();

        public MenuScreen():base(null)
        {
            lstMB.Add(new Menu.menuButton(new Rectangle(150, 400, 100, 20), "Mario"));
            lstMB.Add(new Menu.menuButton(new Rectangle(150, 450, 100, 20), "Link"));
            lstMB.Add(new Menu.menuButton(new Rectangle(150, 500, 100, 20), "Bill Rizer"));
            lstMB.Add(new Menu.menuButton(new Rectangle(150, 550, 100, 20), "Sonic"));
            lstMB[0].selected = true;
        }

        /// <summary>
        /// Seleciona o botão através da seta para baixo e se
        /// o espaço for pressionado leva para tela correspondente
        /// </summary>
        public void keyboardSelect()
        {
            if (Game1.keyboardState.IsKeyDown(Keys.Down) && !Game1.previusKeyboardState.IsKeyDown(Keys.Down))
            {
                for (int i = 0; i < lstMB.Count; i++)
                {
                    if (lstMB[i].selected == true)
                    {
                        Game1.window.Title = lstMB[selectNextButton(i, "next")].text;
                        lstMB[i].selected = false;
                        lstMB[selectNextButton(i, "next")].selected = true;
                        
                        break;
                    }
                }
            }
            if (Game1.keyboardState.IsKeyDown(Keys.Up) && !Game1.previusKeyboardState.IsKeyDown(Keys.Up))
            {
                for (int i = lstMB.Count-1; i >= 0; i--)
                {
                    if (lstMB[i].selected == true)
                    {
                        Game1.window.Title = lstMB[selectNextButton(i, "previous")].text;
                        lstMB[i].selected = false;
                        lstMB[selectNextButton(i, "previous")].selected = true;

                        break;
                    }
                }
            }
            if (Game1.keyboardState.IsKeyDown(Keys.Space) && !Game1.previusKeyboardState.IsKeyDown(Keys.Space))
            {
                for (int i = 0; i < lstMB.Count; i++)
                {
                    if (lstMB[i].text == "Mario" && lstMB[i].selected == true)
                    {
                        ScreenManager.setCurrentScreen(ScreenName.PITFALL_SCREEN);
                    }

                    if (lstMB[i].text == "Link" && lstMB[i].selected == true)
                    {
                        ScreenManager.setCurrentScreen(ScreenName.MEGAMAN_SCREEN);
                    }

                    if (lstMB[i].text == "Bill Rizer" && lstMB[i].selected == true)
                    {
                        ScreenManager.setCurrentScreen(ScreenName.METALSLUG_SCREEN);
                    }

                    if (lstMB[i].text == "Sonic" && lstMB[i].selected == true)
                    {
                        ScreenManager.setCurrentScreen(ScreenName.GOLDENAXE_SCREEN);
                    }
                }
            }
        }


        /// <summary>
        /// Controle das fases usando numPad: 1 = mario, 2 = megaman, 3 = bill rizer, 4 = sonic
        /// </summary>
        #region Screen Control With Numbers
        public static void numPadScreenControl()
        {

           if (Game1.keyboardState.IsKeyDown(Keys.NumPad1) && !Game1.previusKeyboardState.IsKeyDown(Keys.NumPad1))
           {
               ScreenManager.setCurrentScreen(ScreenName.PITFALL_SCREEN);
               
           }

           else if (Game1.keyboardState.IsKeyDown(Keys.NumPad2) && !Game1.previusKeyboardState.IsKeyDown(Keys.NumPad2))
           {
               ScreenManager.setCurrentScreen(ScreenName.MEGAMAN_SCREEN);
           }

           else if (Game1.keyboardState.IsKeyDown(Keys.NumPad3) && !Game1.previusKeyboardState.IsKeyDown(Keys.NumPad3))
           {
               ScreenManager.setCurrentScreen(ScreenName.METALSLUG_SCREEN);
           }

           else if (Game1.keyboardState.IsKeyDown(Keys.NumPad4) && !Game1.previusKeyboardState.IsKeyDown(Keys.NumPad4))
           {
               ScreenManager.setCurrentScreen(ScreenName.GOLDENAXE_SCREEN);
           }
        }
        #endregion

        /// <summary>
        /// Verifica qual o próximo botão de acordo 
        /// com a ordem da lista
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        private int selectNextButton(int position, String direction)
        {
            if("next".Equals(direction)){

                if (position == lstMB.Count - 1)
                {
                    return 0;
                }
                else
                {
                    return ++position;
                }
            }
            else{

                if (position == 0)
                {
                    return lstMB.Count - 1;
                }
                else
                {
                    return --position;
                }
            }
        }

        public override void load(ContentManager content)
        {
            base.load(content);
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            foreach (Menu.menuButton m in lstMB)
            {
                m.Draw(spriteBatch, Game1.fontMenu);
            }

            base.draw(spriteBatch);
        }

        public override void update(GameTime gameTime, GameWindow Window)
        {
            numPadScreenControl();
            keyboardSelect();

            base.update(gameTime, Window);
        }
    }
}
